package cn.jiahua.hospital.controller.adminBack;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.Departments;
import cn.jiahua.hospital.pojo.News;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.DepartmentsService;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "超级管理员后台-一级科室")
@RequestMapping("/api/back/Departments/")
public class DepartmentsBackController {

    @Autowired
    DepartmentsService DepartmentsService;

    @ApiOperation(value = "一级科室分页", produces = "application/json", notes = "根据一级科室名称模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentsName", value = "科室名字", required = false, dataType = "string"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/limit")
    public Result limit(String departmentsName,
                        Integer page,
                        Integer limit) {
        //总数量
        int count = DepartmentsService.departmentSelectCount(departmentsName);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数
        int pageCount = page1.getPages();
        //调用service层获取数据

        List<Departments> departmentsList=DepartmentsService.departmentsSelectPage(departmentsName,(page-1)*limit,limit);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("departmentsList", departmentsList);
        return ResultUtils.returnSuccess("查询成功", map);
    }

    @ApiOperation(value = "查看一级科室", produces = "application/json", notes = "根据一级科室ID查看一级科室详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentsId", value = "一级科室ID", required = true, dataType = "int")
    })
    @GetMapping("/select")
    public Result select(Integer departmentsId) {
        Departments departments = DepartmentsService.getDepartmentsById(departmentsId);
        return ResultUtils.returnDataSuccess(departments);
    }


    @ApiOperation(value = "增加一级科室", produces = "application/json", notes = "增加一级科室信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentsName", value = "科室名称", required = true, dataType = "String")
    })
    @GetMapping("/add")
    public Result add(String departmentsName) {
        Map map=new HashMap();
        Departments departments=new Departments();
        map.put("departmentsName",departmentsName);
        List<Departments>DepartmentsList=DepartmentsService.getDepartmentsListByMap(map);
        if (DepartmentsList != null && DepartmentsList.size() > 0) {
            return ResultUtils.returnFail("请勿重复添加科室");
        }else {
            departments.setDepartmentsName(departmentsName);
            int count = DepartmentsService.qdtxAddDepartments(departments);
            if (count > 0) {
                return ResultUtils.returnSuccess("增加成功");
            }
        }
        return ResultUtils.returnFail();
    }
}
